---@class FighterUtils
FighterUtils = ClientFight.CreateClass("FighterUtils")
local this = FighterUtils
---战士查找
---@param fight Fight 战斗数据
---@param area  number 区域（null不判断）
---@param living boolean 是否活着（null不判断）
---@param size   number 数量（null不判断）
---@param comparator fun(a:Fighter,b:Fighter) 排序器
---@param inclusiveSummons boolean 是否包含召唤物
---@return Fighter[]
function this.getFightersBy( fight,  area,  living,  size,  comparator,  inclusiveSummons)
    if inclusiveSummons == nil then
        inclusiveSummons = true
    end
    ---@type Fighter[]
    local list = {}
    ---@type Fighter[]
    local fighters = {}
    if (area == nil) then
        table.addAllToArr(fighters,fight.teams[1].fighters)
        table.addAllToArr(fighters,fight.teams[2].fighters)
    elseif (area == 1) then
        table.addAllToArr(fighters,fight.teams[1].fighters)
    elseif (area == 2) then
        table.addAllToArr(fighters,fight.teams[2].fighters)
    end
    if (comparator ~= nil) then
        table.sort(fighters,comparator)
    end
    for k,fighter in pairs( fighters) do
        local continue = false
        if (not inclusiveSummons) then
            if (fighter.summons) then
                continue = true;
            end
        end
        if not continue and ((living == nil or living ~= fighter:isDie())) then
            table.insert(list,fighter)
            if (size ~= nil and size <= #list) then
                break;
            end
        end
    end
    return list;
end
---@param fighter Fighter
---@return Fighter[]
function this.getOursBy(fighter, living, size, inclusiveSummons, comparator)
    if inclusiveSummons == nil then
        inclusiveSummons = true
    end
    return this.getFightersBy(fighter.fight, fighter.teamIndex, living, size, comparator, inclusiveSummons);
end
---@param fighter Fighter
---@return Fighter[]
function this.getOthersBy( fighter, living, size, containsSummons)
    if containsSummons == nil then
        containsSummons = true
    end
    return this.getFightersBy(fighter.fight, 3 - fighter.teamIndex, living, size, nil, containsSummons);
end
---根据站位找英雄
---@param fighters Fighter[]
---@return Fighter
function this.getFighterBySite(fighters,site)
    for k,f in pairs(fighters) do
        if (f.site == site) then
            return f
        end
    end
    return nil
end
---@param fight Fight
function this.getFighterBySite1(fight,teamIndex,site)
    return this.getFighterBySite(fight.teams[teamIndex].fighters, site);
end
---获取前方中心点
---@param fighter Fighter
---@param rectangle Rectangle2
function this.getFrontCenter( fighter,  rectangle)
    local centerX = fighter.position.x + rectangle.length / 2 * Global.RIGHT * this.getTeamSiteDirection(fighter);
    return Vector2(centerX, fighter.position.y);
end
---获取后方中心点
function this.getBackCenter(fighter,  rectangle)
    local centerX = fighter.position.x + rectangle.length / 2 * Global.LEFT * this.getTeamSiteDirection(fighter);
    return Vector2(centerX, fighter.position.y);
end
---队伍初始站位的 朝向,  站坐标就是朝向右边  反义亦然
---@param fighter Fighter
function this.getTeamSiteDirection(fighter)
    return fighter.teamIndex == 0 and Global.RIGHT or Global.LEFT
end
---对象当前的真实朝向
---@param fighter Fighter
function this.getCurrDirection(fighter)
    --TODO 暂时先这么写, 有时间,需要实现 Fighter.direction
    local moveDirection = fighter.moveDirection;
    if (moveDirection ~= 0) then
        return moveDirection;
    end
    local movePosition = fighter.movePosition;
    if (movePosition ~= nil) then
        return this.leftOrRight(fighter, movePosition);
    end
    local moveTarget = fighter.moveTarget
    if (moveTarget ~= nil) then
        return this.leftOrRight1(fighter, moveTarget);
    end
    local prepareTarget = fighter.prepareTarget
    if (prepareTarget ~= nil)then
        return this.leftOrRight1(fighter, prepareTarget);
    end
    local usingSkill = fighter.usingSkill
    if (usingSkill ~= nil) then
        local target = usingSkill.target
        if (target ~= nil) then
            return this.leftOrRight1(fighter, target);
        end
    end
    return this.getTeamSiteDirection(fighter);
end
---计算朝向(position相对于fighter）
---@param fighter Fighter
function this.leftOrRight(fighter,position)
    if (fighter.position.x < position.x) then
        return Global.RIGHT;
    elseif (fighter.position.x > position.x) then
        return Global.LEFT;
    else
        return this.getTeamSiteDirection(fighter);
    end
end
---计算朝向(position相对于fighter）
---@param fighter Fighter
---@param target Fighter
function this.leftOrRight1(fighter,target)
    return this.leftOrRight(fighter,target.position)
end
---随机出替补pos
function this.randomAlternatePos(teamIndex)
    local posArr
    if teamIndex == 1 then
        posArr = Global.ALTERNATE_POS_1
    else
        posArr = Global.ALTERNATE_POS_2
    end
    local x = math.random(posArr[1][1],posArr[1][2]+1)
    local y = math.random(posArr[2][2],posArr[2][1]+1)
    return Vector2(x,y)
end
---获取场上存活人数
---@param team FightTeam
---@param fighter boolean 是否包含伙伴
function this.getOnSceneFighterAmount(team,fighter,summon)
    local total = 0;
    for k,f in pairs(team.fighters) do
        local continue = false
        if (f.dieStatus) then
            continue = true
        end
        if (f.summonsType == SummonsType.UN_ENTITY) then
            continue = true
        end
        if not continue then
            if (f.summons and summon) then
                total = total + 1;
            end
            if (not f.summons and fighter) then
                total = total + 1
            end
        end
    end
    return total;
end